package com.naiyumie.ncalculator;

import com.naiyumie.nlib.*;
import java.math.BigDecimal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 전체값에서 일부값은 몇 퍼센트
 */
public class Percent01Activity extends KingWangJjangActivity {
	
	EditText et1 = null, et2 = null, et3 = null;
	BigDecimal partValue;
	String partValueStr = null;
	BigDecimal wholeValue;
	String wholeValueStr = null;
	BigDecimal resultValue;
	String resultValueStr = null;
	String msg = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_percent_01);
		Intent fromIntent = getIntent();
		msg = fromIntent.getStringExtra("MSG");
		et1 = (EditText) findViewById(R.id.et1);
		et2 = (EditText) findViewById(R.id.et2);
		et3 = (EditText) findViewById(R.id.et3);
		et1.addTextChangedListener(partValueFromWholeValueWatcher);
		et2.addTextChangedListener(partValueFromWholeValueWatcher);
		this.setMsg(msg);
		this.callMethod();
		tvTitle.setText(R.string.category02);
	}
	
	private final TextWatcher partValueFromWholeValueWatcher = new TextWatcher() {
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			et3.setText("0");
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		public void afterTextChanged(Editable s) {
			partValueStr = nWidgetTool.stringFromEditText(et1);
			partValue = nWidgetTool.stringToBigDecimalFromEditText(et1);
			wholeValueStr = nWidgetTool.stringFromEditText(et2);
			wholeValue = nWidgetTool.stringToBigDecimalFromEditText(et2);
			if (!partValueStr.equals(null) && !partValueStr.equals("") && !wholeValueStr.equals(null) && !wholeValueStr.equals("")) {
				BigDecimal calculate = partValue.divide(wholeValue, 4, BigDecimal.ROUND_HALF_UP);
				BigDecimal calculateResult = calculate.multiply(new BigDecimal(100));
				resultValueStr = String.valueOf(calculateResult);
				et3.setText(resultValueStr);
			}
		}
	};

}
